package com.tengju.bff.interfaces.covercharge.service.impl;

import com.alibaba.fastjson.JSON;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeEnum;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceLetaxRegistered;
import com.tengju.bff.interfaces.covercharge.entity.dto.ServiceLetaxRegisteredDto;
import com.tengju.bff.interfaces.covercharge.entity.vo.LeTaxRegisteredAndReturnResult;
import com.tengju.bff.interfaces.covercharge.mapper.ServiceLetaxRegisteredMapper;
import com.tengju.bff.interfaces.covercharge.service.IServiceLetaxRegisteredService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoi7.component.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import com.tengju.bff.interfaces.covercharge.entity.vo.ServiceLetaxRegisteredReturnResult;
import com.tengju.bff.interfaces.covercharge.service.letax.impl.LeTaxServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 乐税个体户注册表 服务实现类
 * </p>
 *
 * @author zhaoyi
 * @since 2020-02-24
 */
@Slf4j
@Service
public class ServiceLetaxRegisteredServiceImpl extends ServiceImpl<ServiceLetaxRegisteredMapper, ServiceLetaxRegistered> implements IServiceLetaxRegisteredService {
    @Autowired
    private ServiceLetaxRegisteredMapper serviceLetaxRegisteredMapper;

    @Autowired
    private LeTaxServiceImpl leTaxServiceImpl;

    @Override
    public Map<Integer, Boolean> getNumberCommon(List<ServiceLetaxRegisteredDto> idCodeList){
        Map<Integer, Boolean> paramMap = new HashMap<>();
        if(idCodeList!=null && !idCodeList.isEmpty()){
            List<ServiceLetaxRegisteredDto> lists = serviceLetaxRegisteredMapper.getNumberCommon(idCodeList);
            for (ServiceLetaxRegisteredDto idCode : lists) {
                paramMap.put(idCode.getIdCode(),true);
            }
        }
        return paramMap;
    }

    /**
     * 更新状态
     */
    @Override
    public void updateStatusToEnd(Integer idCode, String platformCode) {
        serviceLetaxRegisteredMapper.updateStatusToEnd(idCode, platformCode);
    }

    /**
     * 获取乐税注册和返回信息
     *
     * @param idCode       代理id
     * @param platformCode 平台id
     */
    @Override
    public LeTaxRegisteredAndReturnResult leTaxRegisteredAndReturnInfo(Integer idCode, String platformCode) {
        log.info("获取乐税注册和返回信息开始,idCode:{},platformCode:{}", idCode, platformCode);
        if (idCode == null) {
            throw new ServiceException("idCode为空");
        }
        if (platformCode == null || "".equals(platformCode)) {
            throw new ServiceException("platformCode为空");
        }
        LeTaxRegisteredAndReturnResult leTaxRegisteredAndReturnResult = serviceLetaxRegisteredMapper.leTaxRegisteredAndReturnInfo(idCode);
        if (Objects.isNull(leTaxRegisteredAndReturnResult)) {
            throw new ServiceException("乐税注册信息结果为空");
        }

        //如果法人身份证为空，则取判断是否是共用一个个体工商户
        if (leTaxRegisteredAndReturnResult.getLegalIdCardNo() == null || "".equals(leTaxRegisteredAndReturnResult.getLegalIdCardNo())) {
            List<ServiceLetaxRegisteredDto> idCodeList = new ArrayList<>();
            idCodeList.add(new ServiceLetaxRegisteredDto(idCode));
            Map<Integer, Boolean> commonMap = new HashMap<>();
            try {
                commonMap = this.getNumberCommon(idCodeList);
            } catch (Exception e) {
                log.error("获取commonMap异常:", e);
            }
            log.info("如果法人身份证为空,commonMap:{}", JSON.toJSONString(commonMap));

            LeTaxRegisteredAndReturnResult leTaxRegisteredAndReturn = null;
            //是共用一个个体工商户，则去另一个找另一个平台信息
            if (commonMap.containsKey(idCode)) {
                //账户类型
                String platformCodes = CoverChargeEnum.ServiceAccountType.MANAGE_TYPE.getPlatform() ;
                leTaxRegisteredAndReturn = serviceLetaxRegisteredMapper.leTaxRegisteredAndReturnInfo(idCode);
            }
            if (Objects.isNull(leTaxRegisteredAndReturn)) {
                throw new ServiceException("乐税共用个体工商户结果为空");
            }
            return leTaxRegisteredAndReturn;
        }
        return leTaxRegisteredAndReturnResult;
    }

    /**
     * 获取乐税注册和返回信息
     *
     * @param id  id
     */
    @Override
    public LeTaxRegisteredAndReturnResult leTaxRegisteredAndReturnInfoById(Integer id) {
        log.info("获取乐税注册和返回信息开始,id:{}", id);
        if (id == null) {
            throw new ServiceException("id为空");
        }
        LeTaxRegisteredAndReturnResult leTaxRegisteredAndReturnResult = serviceLetaxRegisteredMapper.leTaxRegisteredAndReturnInfoById(id);
        if (Objects.isNull(leTaxRegisteredAndReturnResult)) {
            throw new ServiceException("乐税注册信息结果为空");
        }
        return leTaxRegisteredAndReturnResult;
    }

    @Override
    public String getToken(){
        return leTaxServiceImpl.getToken();
    }

    @Override
    public ServiceLetaxRegisteredReturnResult getMerchantInfo(Integer idCode){
        return leTaxServiceImpl.searchMerchantInfo(idCode);
    }

    @Override
    public String h5Login(Integer idCode){
        return leTaxServiceImpl.h5Login(idCode);
    }

    @Override
    public String registeredCode(Integer idCode) {
        return leTaxServiceImpl.registeredCode(idCode);
    }
}
